package com.v1.model;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class BaseModel {
	protected final Log log = LogFactory.getLog(getClass());
	private int startIndex;
	private int offset;
	private int rowCount;
	private int rowsPerPage;
	private int pageNo;
	private int pageCount;
	private int pageIndexCount = 10;

	public void init(int rowCount, int pageNo, int rowsPerPage, int pageIndexCount) {
		this.rowCount = rowCount;
		this.rowsPerPage = rowsPerPage;
		this.pageNo = pageNo;
		this.pageIndexCount = pageIndexCount;
		init();
	}

	public void init(int rowCount, int pageNo, int rowsPerPage) {
		init(rowCount, pageNo, rowsPerPage, 10);
	}

	public void init(int rowCount, int pageNo) {
		init(rowCount, pageNo, 10);
	}

	private void init() {
		if (rowCount == 0) {
			return;
		}

		if (rowsPerPage > rowCount) {
			pageNo = 1;
			offset = rowCount;
			startIndex = 0;
			pageCount = 1;
			return;
		}

		double value = (double) rowCount / (double) rowsPerPage;
		pageCount = (int) Math.ceil(value);

		if (pageCount <= pageNo) {
			pageNo = pageCount;
			offset = rowCount - (pageNo - 1) * rowsPerPage;
			startIndex = (pageNo - 1) * rowsPerPage;
			return;
		}

		offset = rowsPerPage;
		startIndex = (pageNo - 1) * rowsPerPage;

	}

	public int getStartIndex() {
		return startIndex;
	}

	public int getOffset() {
		return offset;
	}

	public int getRowCount() {
		return rowCount;
	}

	public int getRowsPerPage() {
		return rowsPerPage;
	}

	public int getPageNo() {
		return pageNo;
	}

	public int getPageCount() {
		return pageCount;
	}

	public int getPageIndexCount() {
		return pageIndexCount;
	}

	public static void main(String[] args) {
		int a = 5, b = 3;
		System.out.println(a / b);
		double value = (double) a / (double) b;
		System.out.println((int) Math.ceil(value));

	}
}
